//
//  LoginByAccountView.m
//  YunDongGym
//
//  Created by lqq on 2018/6/12.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "LoginByAccountView.h"
#import "I_Account.h"


@interface LoginByAccountView ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *accountBackView;
@property (weak, nonatomic) IBOutlet UITextField *accountTextFiled;
@property (weak, nonatomic) IBOutlet UIView *pwdBackView;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end
@implementation LoginByAccountView

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    [_accountTextFiled setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"账号" attributes:@{NSForegroundColorAttributeName:HEXColor(@"4A4A4A")}]];
    
    [_pwdTextField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"密码" attributes:@{NSForegroundColorAttributeName:HEXColor(@"4A4A4A")}]];
    
    
    NSArray *needCornerRadiusViews = @[_accountBackView,_pwdBackView,_loginBtn];
    for (UIView *view in needCornerRadiusViews) {
        view.layer.cornerRadius = 2;
        view.layer.masksToBounds = YES;
    }
    
    [_accountTextFiled addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    [_pwdTextField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _accountTextFiled.delegate = self;
    _accountTextFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    _pwdTextField.delegate = self;
    
    //登录按钮设置
    [_loginBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"3AA7FF"] size:CGSizeMake(1, 1)] forState:UIControlStateNormal];
    [_loginBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"DDDDDD"] size:CGSizeMake(1, 1)] forState:UIControlStateDisabled];
    _loginBtn.enabled = NO;
    
#ifdef DEBUG
    _accountTextFiled.text = @"admin123@yd";
    _pwdTextField.text = @"888888";
    _loginBtn.enabled = YES;

#else

#endif
}



- (IBAction)changeToMobileLogin:(id)sender {
    if (_changeToMobileBlock) {
        _changeToMobileBlock();
    }
}
- (IBAction)loginBtnClicked:(id)sender {
    [self endEditing:YES];
    __weak __typeof (self)weakSelf = self;
    NSString *account = _accountTextFiled.text;
    NSString *PWD = _pwdTextField.text;


    [AV showLoading:@"正在登陆"];
    [I_Account loginWithAccount:account PWD:PWD success:^(M_Account *account) {
        [AV hideen];
        [weakSelf loginSuccessWithAccount:account];
    } failure:^(NSError *error) {
        if (weakSelf.loginErrorBlock) {
            weakSelf.loginErrorBlock(error);
        }
    }];
}

- (void)loginSuccessWithAccount:(M_Account *)account
{
    if (_loginSuccessBlock) {
        _loginSuccessBlock(account);
    }
}
#pragma mark - KVO
- (void)textFieldChanged:(UITextField *)textField
{

    if (_pwdTextField.text.length >= 6) {
        _loginBtn.enabled = YES;
    } else {
        _loginBtn.enabled = NO;
    }
    
}
@end
